package com.lvyq.myplayer.ui.activity

import android.content.Context
import android.widget.RadioGroup
import androidx.viewpager.widget.ViewPager
import cn.jzvd.Jzvd
import cn.jzvd.JzvdStd
import com.google.gson.Gson
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.MvPlayAdapter
import com.lvyq.myplayer.base.BaseActivity
import com.lvyq.myplayer.parcelable.MvPagerBeansData
import com.lvyq.myplayer.parcelable.SongInfoData
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find


/**
 * 文件描述：VideoPlayerActicity
 * 作者：lvyq
 * 创建时间：2025-02-26
 * 修改时间：2025-02-26
 */
class VideoPlayerActivity : BaseActivity() {

    val sharedPreferences by lazy { getSharedPreferences("mainMusic", Context.MODE_PRIVATE) }//私有模式（仅当前应用可访问）

    var songInfoData: SongInfoData?=null
    var musicLst :ArrayList<SongInfoData>?=null

    override fun getLayoutId(): Int {
        return R.layout.activity_video
    }

    init {


    }
    override fun initData() {

//        获取数据
        var data: MvPagerBeansData? = intent.getParcelableExtra("data")
        var videoPlay = find<JzvdStd>(R.id.videoPlay)

        videoPlay.setUp(data?.url,data?.title)
        //设置适配缩略图
        Picasso.with(this)
            .load(data?.img)
            .into(videoPlay.posterImageView);

        //模拟点击，自动播放
        videoPlay.startButton.performClick()


    }



    override fun onBackPressed() {
        if (Jzvd.backPress()) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        Jzvd.releaseAllVideos()
    }

    override fun initListener() {
        //适配viewpager
        var data: MvPagerBeansData? = intent.getParcelableExtra("data")
        var id = intent.getStringExtra("title").toString()
        var viewPagerPlay = findViewById<ViewPager>(R.id.viewPagerPlay)
        viewPagerPlay.adapter= MvPlayAdapter(data,supportFragmentManager)
        //radioGroup监听-点击按钮切换fragment
        var radioGroup = find<RadioGroup>(R.id.rg)
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->viewPagerPlay.setCurrentItem(0)
                R.id.rb2->viewPagerPlay.setCurrentItem(1)
            }
        }

        //viewPage监听 -滑动切换之对应的radioButton
        viewPagerPlay.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
//            滑动回调
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int,
            ) {
                     println("滑动回调>>position:$position   positionOffset:$positionOffset   positionOffsetPixels:$positionOffsetPixels")
            }

            //选中状态改变
            override fun onPageSelected(position: Int) {
                println("选中状态改变>>"+position)
                when(position){
                    0->radioGroup.check(R.id.rb1)
                    1->radioGroup.check(R.id.rb2)

                }
            }

            //滑动状态改变
            override fun onPageScrollStateChanged(state: Int) {
                println("滑动状态改变>>"+state)

            }

        })
    }




    override fun onDestroy() {
        super.onDestroy()
    }

    fun getSongInfo(key: String): SongInfoData? {
        val json = sharedPreferences.getString(key, "") ?: return null
        return try {
            Gson().fromJson(json, SongInfoData::class.java)
        } catch (e: Exception) {
            null
        }
    }

    fun getSongInfoList(key: String): ArrayList<SongInfoData> {
        val json = sharedPreferences.getString(key, "") ?: return ArrayList()
        return try {
            // 将解析结果强制转换为 ArrayList
            Gson().fromJson(json, Array<SongInfoData>::class.java).toCollection(ArrayList())
        } catch (e: Exception) {
            ArrayList()
        }
    }
}